﻿using System.Speech.Recognition;
using System.Windows;

namespace SpeechSampleWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        SpeechRecognitionEngine recognizer;

        public MainWindow()
        {
            InitializeComponent();

            recognizer = new SpeechRecognitionEngine();
            recognizer.SetInputToDefaultAudioDevice();

            Grammar grammar = new Grammar("grammar.xml", "thema");
            recognizer.UnloadAllGrammars();
            recognizer.LoadGrammar(grammar);

            recognizer.SpeechRecognized += (s, e1) =>
            {
                string text = e1.Result.Text;
                _recognizedText.Text = text;
            };

        }

        ~MainWindow()
        {
            recognizer.RecognizeAsyncStop();
        }

        private void _speak_Click(object sender, RoutedEventArgs e)
        {
            System.Speech.Synthesis.SpeechSynthesizer synthesizer =
                new System.Speech.Synthesis.SpeechSynthesizer();

            //synthesizer.Speak("Hello World");
            synthesizer.Speak(_text.Text);
            
        }

        private void _startListen_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                recognizer.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch
            {
            }
        }

        private void _stopListen_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                recognizer.RecognizeAsyncStop();
            }
            catch
            {
            }
        }
    }
}
